package com.hengtiansoft.lfy.download;

import android.os.Environment;
import android.widget.Toast;

import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.RequestQueue;
import com.duowan.mobile.netroid.request.FileDownloadRequest;
import com.duowan.mobile.netroid.toolbox.FileDownloader;
import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.net.Netroid;
import com.hengtiansoft.lfy.utils.FileUtil;

import java.io.File;


/**
 * Created by taoli on 2016/4/21.
 */
public class DownloadManager {

    private static FileDownloader mDownloder;
    public static final String mSaveDirPath = Environment.getExternalStorageDirectory() + "/lfy/";
    public static final String mSaveTranslateDirPath = Environment.getExternalStorageDirectory() + "/lfy/translate/";
    public static String mSaveZipFile = mSaveDirPath + "translate.zip";

    private static String downloadBaseUri = "";
    private static String downloadTranslationUri = downloadBaseUri + "http://58.210.52.235:8080/lfy-api/images/zip/1.zip";

    public static final int TRANSLATION = 0;

    private static DownloadManager self = null;

    public DownloadManager() {
        init();
    }

    public static DownloadManager getInstance() {

        if (null == self) {
            self = new DownloadManager();
        }
        return self;
    }

    private void init() {
        RequestQueue queue = Netroid.newRequestQueue(LFYApplication.getInstance().getApplicationContext(), null);
        mDownloder = new FileDownloader(queue, 1) {
            @Override
            public FileDownloadRequest buildRequest(String storeFilePath, String url) {
                return new FileDownloadRequest(storeFilePath, url) {
                    @Override
                    public void prepare() {
                        addHeader("Accept-Encoding", "identity");
                        super.prepare();
                    }
                };
            }
        };

        createFile();
    }

    private static void createFile() {
        File downloadDir = new File(mSaveDirPath);
        if (!downloadDir.exists()) downloadDir.mkdir();
        File downloadQuranDir = new File(mSaveTranslateDirPath);
        if (!downloadQuranDir.exists()) downloadQuranDir.mkdir();
        if(new File(mSaveZipFile).exists())
            new File(mSaveZipFile).delete();
    }

    public static FileDownloader.DownloadController download(int type, String fileName, String url, Listener<Void> listener) {
        FileDownloader.DownloadController controller = null;
        if (!FileUtil.hasSDCard()) {
            Toast.makeText(LFYApplication.getInstance().getApplicationContext(), "SD卡无法使用", Toast.LENGTH_SHORT);
            return controller;
        }
        createFile();
        switch (type) {
            case TRANSLATION:
                controller = mDownloder.add(fileName, url, listener);
                break;
            default:
                break;
        }
        return controller;
    }

    public void clearAllDownloadTask()
    {
        mDownloder.clearAll();
    }
}
